// ==UserScript==
// @namespace siur2@yahoo.com
// @name Pardus hack share
// @include http://*.pardus.at/hack.php*
// ==/UserScript==

var servers = [
{
name: "Falcon Logger",
url: "http://logger.fedfalcons.com",
accounts: {orion: {name: "FalCLogG3R", password: "1099pWd"}}
},
{
name: "Federation Hack Logger",
url: "http://federation.bernieke.com/infocenter",
accounts: {orion: {name: "iNfOCenTer", password: "REtENcOFnI"}}
}
];

function saveHack() {
var server = servers[document.getElementById("sltServer").selectedIndex];

function prepareHack(hack, account) {
function prepareSector(pos, sector) {
var i = sector.indexOf("[");
if (i != -1) {
pos.sector = sector.substr(0, i - 1);
pos.coords = sector.substr(i);
} else
pos.sector = sector;
}

function getPosition(posCell) {
var len = posCell.childNodes.length;
var result = {sector: null, coords: null};
result.cluster = posCell.childNodes[len - 1].textContent;
if (len > 1)
prepareSector(result, posCell.childNodes[0].nodeValue);
return result;
}

function prepareResources(resourceCell) {
var result = [];
var res = null;
for (var i = 0; i < resourceCell.childNodes.length; i++) {
var node = resourceCell.childNodes[i];
var tag = node.tagName;
if (tag) {
if (tag.toLowerCase() == "br")
continue;
else
if (tag.toLowerCase() == "img") {
var name = node.src;
name = name.substr(name.lastIndexOf("/") + 1);
res = {img: name};
}
}
else {
var amount = node.nodeValue;
res.amount = amount.substring(amount.indexOf(" ") + 1, amount.length - 1);
result.push(res);
}
}
return result;
}

var btnShare = document.getElementById("btnShare");
btnShare.disabled = "true";
btnShare.className = "disabled";

var str = document.location.hostname;
var universe = str.substring(0, str.indexOf("."));
if (!universe)
universe = "orion";
var acc = server.accounts[universe];
if (!acc)
return false;
account.name = acc.name;
account.password = acc.password;
hack.universe = universe.charAt(0).toUpperCase() + universe.substring(1);

var table = document.getElementsByTagName("table")[0];
table = table.rows[1].cells[1].getElementsByTagName("table")[0];
var cell = table.rows[0].cells[0];
var tables = [];
for (var i = 0; i < cell.childNodes.length; i++) {
var child = cell.childNodes[i];
var tag = child.tagName;
if (tag && (tag.toLowerCase() == "table"))
tables.push(child);
}
hack.location = table.getElementsByTagName("a")[0].innerHTML;

var buildingTable = null;
var foeTable = null;
var friendTable = null;
var resourceTable = null;
var shipTable = null;

switch (tables.length) {
case 2:
hack.method = "brute";
break;
case 4:
hack.method = "skilled";
buildingTable = tables[2];
foeTable = tables[3];
break;
case 5:
hack.method = "freak";
buildingTable = tables[2];
foeTable = tables[3].getElementsByTagName("table")[0];
friendTable = tables[3].getElementsByTagName("table")[1];
resourceTable = tables[4];
break;
case 6:
hack.method = "guru";
shipTable = tables[2];
buildingTable = tables[3];
foeTable = tables[4].getElementsByTagName("table")[0];
friendTable = tables[4].getElementsByTagName("table")[1];
resourceTable = tables[5];
break;
default:
return false;
}

var pilotTable = tables[1];
hack.date = new Date(pilotTable.rows[0].cells[0].innerHTML.replace(/-/g, "/")).getTime();
hack.pilot = pilotTable.rows[1].cells[0].textContent;
hack.credits = pilotTable.rows[3].cells[0].innerHTML.replace(/,/g, "");
hack.reputation = pilotTable.rows[3].cells[1].innerHTML.replace(/,/g, "");
hack.building_amount = pilotTable.rows[3].cells[2].innerHTML;
if (pilotTable.rows.length >= 6)
hack.experience = pilotTable.rows[5].cells[0].innerHTML.replace(/,/g, "");
if (pilotTable.rows.length >= 8)
hack.position = getPosition(pilotTable.rows[7].cells[0]);

var lookupCell = tables[0].rows[1].cells[0];
var pilotSelect = lookupCell.getElementsByTagName("select")[0];
if (pilotSelect) {
for (var i = 0; i < pilotSelect.length; i++) {
var option = pilotSelect.item(i);
if (option.text == hack.pilot) {
hack.pilot_id = option.value;
break;
}
}
} else {
var pilotInput = lookupCell.getElementsByTagName("input")[1];
hack.pilot_id = pilotInput.value;
}

if (shipTable != null) {
hack.ship_status = {};
for (var i = 1; i < shipTable.rows.length; i++) {
var font = shipTable.rows[i].cells[0].getElementsByTagName("font")[0];
var name = font.innerHTML;
name = name.substr(0, name.indexOf(":")).toLowerCase();
var status = {};
status.color = font.color;
status.amount = shipTable.rows[i].cells[1].getElementsByTagName("table")[0].width;
hack.ship_status[name] = status;
}
}

hack.building_positions = [];
if (buildingTable != null) {
for (var i = 1; i < buildingTable.rows.length; i++) {
var pos = getPosition(buildingTable.rows[i].cells[1]);
pos.amount = buildingTable.rows[i].cells[0].innerHTML;
hack.building_positions.push(pos);
}
}

hack.foes = [];
hack.foe_alliances = [];
var alliance = false;
if (foeTable != null) {
for (var i = 1; i < foeTable.rows.length; i++) {
var cell = foeTable.rows[i].cells[0];
if (cell.tagName.toLowerCase() == "th") {
alliance = true;
continue;
}
if (alliance)
hack.foe_alliances.push(cell.textContent);
else
hack.foes.push(cell.textContent);
}
}

hack.friends = [];
hack.friend_alliances = [];
alliance = false;
if (friendTable != null) {
for (var i = 1; i < friendTable.rows.length; i++) {
var cell = friendTable.rows[i].cells[0];
if (cell.tagName.toLowerCase() == "th") {
alliance = true;
continue;
}
if (alliance)
hack.friend_alliances.push(cell.textContent);
else
hack.friends.push(cell.textContent);
}
}

if (resourceTable) {
hack.buildings = [];
var buildingTables = resourceTable.getElementsByTagName("table");
for (var i = 0; i < buildingTables.length; i++) {
var posStr = buildingTables[i].rows[0].cells[0].innerHTML;
posStr = posStr.substr(new String("Building in ").length);
var building = {};
prepareSector(building, posStr);
for (var j = 0; j < hack.building_positions.length; j++)
if (building.sector == hack.building_positions[j].sector) {
building.cluster = hack.building_positions[j].cluster;
break;
}
building.commodities = prepareResources(buildingTables[i].rows[2].cells[0]);
building.stock = prepareResources(buildingTables[i].rows[2].cells[1]);
hack.buildings.push(building);
}
}

return true;
}

function objectToXml(obj, nodeName, parentNode) {
if (!parentNode)
parentNode = document.implementation.createDocument("", "", null);
var node = document.createElement(nodeName);
parentNode.appendChild(node);
if (obj instanceof Array) {
var collection = node.ownerDocument.createAttribute("collection");
collection.value = true;
node.attributes.setNamedItem(collection);
var name = nodeName.substr(0, nodeName.length - 1);
for (var i = 0; i < obj.length; i++)
objectToXml(obj[i], name, node);
} else
if (typeof(obj) == "object") {
var i;
for (i in obj)
objectToXml(obj[i], i, node);
} else
node.appendChild(node.ownerDocument.createTextNode(obj));
return parentNode;
}

function xmlToStr(doc) {
return new XMLSerializer().serializeToString(doc);
}

function postHack(data, account) {
function appendFormElement(form, elemName, type, name, value) {
var elem = document.createElement(elemName);
if (type)
elem.type = type;
elem.name = name;
elem.value = value;
form.appendChild(elem);
}

var frame = document.getElementById("hackFrame");
if (!frame) {
frame = document.createElement("iframe");
frame.name = "hackFrame";
frame.id = frame.name;
frame.style.display = "none";
document.body.appendChild(frame);
}
var form = document.getElementById("hackPost");
if (form)
document.body.removeChild(form);
form = document.createElement("form");
form.method = "post";
form.action = server.url + "/hack_add.php";
form.target = "hackFrame";
form.id = "hackPost";
form.style.display = "none";
appendFormElement(form, "textarea", null, "data", data);
appendFormElement(form, "input", "text", "acc", account.name);
appendFormElement(form, "input", "text", "pwd", account.password);
document.body.appendChild(form);
form.submit();
}

var hack = {};
var account = {};
if (!prepareHack(hack, account))
return;
var doc = objectToXml(hack, "hack");
postHack(xmlToStr(doc), account);
}

function sltServer_onChange() {
var btnShare = document.getElementById("btnShare");
btnShare.disabled = false;
btnShare.className = "";
}

function addShareBtn() {
var label = document.createElement("b");
label.appendChild(document.createTextNode("Share the hack at: "));

var btnShare = document.createElement("input");
btnShare.id = "btnShare";
btnShare.type = "button";
btnShare.value = "Share";
btnShare.addEventListener("click", saveHack, false);

var select = document.createElement("select");
select.id = "sltServer";
for (var i = 0; i < servers.length; i++) {
var option = document.createElement("option");
option.text = servers[i].name;
option.value = i;
select.appendChild(option);
}
select.addEventListener("change", sltServer_onChange, false);

var table = document.getElementsByTagName("table")[0];
table = table.rows[1].cells[1].getElementsByTagName("table")[0];
table = table.getElementsByTagName("table")[0];
var row = table.insertRow(-1);
var cell = row.insertCell(-1);
cell.colSpan = 2;
cell.align = "center";
cell.appendChild(label);
cell.appendChild(select);
cell.appendChild(document.createTextNode(" "));
cell.appendChild(btnShare);
}

addShareBtn();